﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;

namespace DictationToolSilverlight.View
{
    [TemplatePart(Name = InlinesEx.ElementTextName, Type = typeof(TextBlock))]
    [TemplatePart(Name = InlinesEx.ElementStrikeLineName, Type = typeof(Line))]
    public class InlinesEx : Control
    {
        internal const string ElementTextName = "TextBlock";

        internal const string ElementStrikeLineName = "StrikeLine";

        public InlinesEx(string text)
            : base()
        {
            this.text = text;
            this.DefaultStyleKey = typeof(InlinesEx);
        }

        public string Text
        {
            get { return text; }
        }

        public bool StrikeLineEnable
        {
            get { return (bool)GetValue(StrikeLineEnableProperty); }
            set { SetValue(StrikeLineEnableProperty, value); }
        }

        // Using a DependencyProperty as the backing store for StrikeLineEnable.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StrikeLineEnableProperty =
            DependencyProperty.Register("StrikeLineEnable", typeof(bool), typeof(InlinesEx), new PropertyMetadata(false, OnStrikeLineEnableChanged));

        private static void OnStrikeLineEnableChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            InlinesEx inline = obj as InlinesEx;
            if (inline != null && inline.strikeThroughLine != null)
            {
                if ((bool)args.NewValue)
                {
                    inline.strikeThroughLine.Visibility = Visibility.Visible;
                }
                else
                {
                    inline.strikeThroughLine.Visibility = Visibility.Collapsed;
                }
            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            textBlock = GetTemplateChild(ElementTextName) as TextBlock;
            strikeThroughLine = GetTemplateChild(ElementStrikeLineName) as Line;

            if (textBlock != null)
            {
                textBlock.SizeChanged += new SizeChangedEventHandler(textBlock_SizeChanged);
                textBlock.Text = text;
            }

            if (strikeThroughLine != null)
            {
                strikeThroughLine.Visibility = StrikeLineEnable ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        void textBlock_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (StrikeLineEnable)
            {
                strikeThroughLine.Stroke = this.Foreground;
                strikeThroughLine.X1 = 0;
                strikeThroughLine.Y1 = textBlock.ActualHeight / 2;
                strikeThroughLine.X2 = textBlock.ActualWidth - 1;
                strikeThroughLine.Y2 = strikeThroughLine.Y1;
            }
        }

        private TextBlock textBlock;
        private Line strikeThroughLine;
        private string text;
    }
}
